package com.bank.service;

import com.alibaba.druid.pool.DruidDataSource;
import com.bank.model.User;
import com.bank.utils.CRUD;
import com.bank.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class VerifyService {

    @Autowired
    CRUD op;

    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 验证登录
     * @param account 账号（20 11 18）
     * @param password（转MD5判断）
     * @return 成功{"code":"200","user":User},失败{"code":"400","msg":"失败原因"}
     */
    public Map<String,Object> verifyLogin(String account,String password){
        Map<String,Object> map = new HashMap<>();
        switch(account.length()){
            case 20:
                String user_psd = op.findPasswordByUid(account);
                if(user_psd.length() == 0){
                    map.put("code","400");
                    map.put("msg","用户不存在");
                }else if(password.equals(user_psd)){
                    map.put("code","200");
                    map.put("user",op.getUserByUid(account));
                }else{
                    map.put("code","400");
                    map.put("msg","密码错误");
                }
                break;
            case 18 :
                String user_id_card = op.findUidByIdCard(account);
                if(user_id_card.length() == 0){
                    map.put("code","400");
                    map.put("msg","该身份证号用户不存在");
                }else{
                    if(password.equals(op.findPasswordByUid(user_id_card))){
                        map.put("code","200");
                        map.put("user",op.getUserByUid(user_id_card));
                    }else{
                        map.put("code","400");
                        map.put("msg","密码错误");
                    }
                }
                break;
            case 11:
                String user_id_phone = op.findUidByPhoneNumber(account);
                if(user_id_phone.length() == 0){
                    map.put("code","400");
                    map.put("msg","该手机号用户不存在");
                }else{
                    if(password.equals(op.findPasswordByUid(user_id_phone))){
                        map.put("code","200");
                        map.put("user",op.getUserByUid(user_id_phone));
                    }else{
                        map.put("code","400");
                        map.put("msg","密码错误");
                    }
                }
                break;
            default:
                map.put("code","400");
                map.put("msg","输入格式错误");
                break;

        }
        return map;
    }

    /**
     * 验证注册
     * @param map 注册信息
     * @return 成功{"code":"200"},失败{"code":"400","msg":"失败原因"}
     */
    public Map<String,String> verifyRegister(Map<String, String> map){
        String name = map.get("name");
        String phone = map.get("phone");
        String idCard = map.get("idCard");
        String email = map.get("email");
        String psd = map.get("password");
        Map<String,String> ret = new HashMap<>();
        if(op.findUidByIdCard(idCard).length() != 0){
            ret.put("code","400");
            ret.put("msg","该身份证号已存在");
        }else if(op.findUidByPhoneNumber(phone).length() != 0){
            ret.put("code","400");
            ret.put("msg","该手机号已存在");
        }else if(op.findUidByEmail(email).length() != 0){
            ret.put("code","400");
            ret.put("msg","该邮箱已存在");
        }else{
            String new_uid = snowflakeIdWorker.nextId() + "00";
            op.addNewUser(new_uid,name,phone,idCard,email,psd);
            ret.put("code","200");
            ret.put("uId",new_uid);
            ret.put("email",email.equals("@")?"":email);
        }
        return ret;
    }

    /**
     * 验证能否办理银行卡
     * @param uid
     * @param type 0 信用卡 1 储蓄卡（验证 <5）
     * @return
     */
    public Map<String,String> verifyCards(String uid,int type,String psd){
        Map<String,String> map = new HashMap<>();
        if(type == 1){
            if(op.getCardsCountByUid(uid) >= 5){
                map.put("code","400");
                map.put("msg","储蓄卡数量已达上限(5张)");
                return map;
            }
        }
        map.put("code","200");
        String newCardId = snowflakeIdWorker.nextId() + "44";
        map.put("msg",newCardId);
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
        op.addNewCard(newCardId,type,psd,date,uid,
                type == 0 ? new BigDecimal("1000") : new BigDecimal("0"));
        return map;
    }

}
